package com.li.particle
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Rectangle;

/*
    Renders a set of 2D points in a 2D bitmap canvas.
 */
public class PointRenderer extends Sprite
{
    private var _width:Number;
    private var _height:Number;
    private var _bmdRect:Rectangle;
    private var _bgColor:uint;
    private var _pointColor:uint;
    private var _bmp:Bitmap;
    private var _bmd:BitmapData;

    public function PointRenderer(width:Number, height:Number, bgColor:uint = 0x000000, pointColor:uint = 0xFFFFFF)
    {
        mouseEnabled = mouseChildren = false;

        _width = width;
        _height = height;
        _bgColor = bgColor;
        _pointColor = pointColor;

        _bmd = new BitmapData(_width, _height, false, _bgColor);
        _bmdRect = _bmd.rect;
        _bmp = new Bitmap(_bmd);
        addChild(_bmp);
    }

    public function renderPoints(points:Vector.<Number>):void
    {
        _bmd.lock();
        _bmd.fillRect(_bmdRect, _bgColor);

        var pixels:Vector.<uint> = _bmd.getVector(_bmdRect);

        var i:uint;
        var loop:uint = points.length/2;
        var px:uint, py:uint;
        var out:int;
        var index:uint;
        for(i = 0; i < loop; ++i)
        {
            index = 2*i;
            px = points[index];
            py = points[index + 1];
            out = int(_width*py) + int(px);
            pixels[out] = _pointColor;
        }

        _bmd.setVector(_bmdRect, pixels);
        _bmd.unlock();
    }
}
}